<%@page import="java.util.*"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
    
<%@page import="com.utility.*,com.bean.*" %>    
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>My Mobile Shop</title>
</head>
<body>

<%! MobileDAO mobileDAO = new MobileDAO();  int price;%>

<%  
	int id; 
	String model = request.getParameter("model").trim();
	String company = request.getParameter("company").trim();
	String camera = request.getParameter("camera").trim();
	
	ArrayList<String> validationMessages = new ArrayList<String>();
	
	if (model == null || model.equals("") || !model.matches("[a-zA-Z0-9 ]+"))
		validationMessages.add("<font size='4' color='red' > Wrong Model </font> <br />");
	
	if (company == null || company.equals("") || !company.matches("[a-zA-Z0-9 ]+"))
		validationMessages.add("<font size='4' color='red' > Wrong Company </font> <br />");
	
	if (camera == null || camera.equals("") || !camera.matches("[a-zA-Z0-9 ]+"))
		validationMessages.add("<font size='4' color='red' > Wrong Camera </font> <br />");
	
	try
	{
		price = Integer.parseInt(request.getParameter("price").trim());
	}
	catch(Exception ex)
	{
		validationMessages.add("<font size='4' color='red' > Wrong Price </font> <br />");
	}
	
	if (!validationMessages.isEmpty())
	{
		request.setAttribute("validationMessages",validationMessages);
		request.getRequestDispatcher("displayError.jsp").forward(request,response);
		//return;
	}
	else
	{
		Mobile mobile = new Mobile(model, company, camera, price);
		
		int afftectedRows = mobileDAO.insert(mobile);
		
		if (afftectedRows > 0)
		{
			out.print("<font size='4' color='green' > Mobile Inserted Successfully </font>");
		}
		else
			out.print("<font size='4' color='red' > Something went wrong </font>");
	}
%>
<br />
<a href="javascript:history.back()"> Back </a>

</body>
</html>